/**
 * @Author: lixiaojun
 * @Date: 2020/5/20 9:50
 */
package main

import (
	"context"
	"strconv"

	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/kcgin/logs"
	"git.900sui.cn/kc/rabbitmq"
	"git.900sui.cn/kc/rpcinterface/client/comtreeData"
	"git.900sui.cn/kc/rpcinterface/client/risk"
	"github.com/streadway/amqp"

	"git.900sui.cn/kc/rpcinterface/client/elastic/bus"
	comtreeData2 "git.900sui.cn/kc/rpcinterface/interface/comtreeData"
)

func main() {

	rpcbbaa := new(risk.BusBasicAccumulative).Init()
	defer rpcbbaa.Close()
	rpcComtreeData := new(comtreeData.Consumption).Init()
	defer rpcComtreeData.Close()

	logs.Info("Environment Variable:MSF_ENV:", kcgin.KcConfig.RunMode)
	err := rabbitmq.Rabbitmq.Consume(kcgin.AppConfig.String("queue.setBus"), func(msg amqp.Delivery) {
		busId, err := strconv.Atoi(string(msg.Body))
		if err != nil {
			return
		}
		rpcBus := new(bus.Bus).Init()
		defer rpcBus.Close()
		rpcMerchantsSettled := new(comtreeData.MerchantSettled).Init()
		defer rpcMerchantsSettled.Close()
		reply := false
		// 通过消息队列将企业/商户信息  添加/更新到ES
		if err := rpcBus.SetAdminBus(context.Background(), &busId, &reply); err != nil {
			logs.Error("SetAdminBus error: %#v\n", err.Error())
			return
		}
		//商户入驻-merchants_settled
		if err = rpcMerchantsSettled.AddMerchantSettledRpc(context.Background(), &comtreeData2.ArgsMerchantSettled{BusId: busId}, &reply); err != nil {
			logs.Error("添加商户入驻到comtreeData，失败信息：\n", busId, err.Error())
		}
		//  添加 预付卡风险管理系统--riskId
		if err = rpcbbaa.BusBasicRisId(context.Background(), busId, &reply); err != nil {
			logs.Error("BusBasicRisId error: %#v\n", err.Error())
		}
		// 添加 全国市场规模
		if err = rpcComtreeData.AddScaleNationalMarketRpc(context.Background(), &busId, &reply); err != nil{
			logs.Error("添加 全国市场规模: %#v\n", err.Error())
		}
		// 添加 业务规模 信息
		if err = rpcComtreeData.AddScaleBusinessRpc(context.Background(), &busId, &reply); err != nil {
			logs.Error("添加 业务规模 信息: %#v\n", err.Error())
		}

		return
	})
	if err != nil {
		panic(err)
	}
}
